import { createRouter,createWebHistory } from 'vue-router'
import { page1 } from './page1/inex'
import { page2 } from './page2/inex'
import { page3 } from './page3/inex'
import { ElMessage } from 'element-plus'
import { useMainStore } from '@/store'
// const store = useMainStore();

const routes = [
    {
        path: '/',
        name: 'login',
        component: () => import('@/views/login.vue'),
        meta: {
            auth:[],
            title: '登录页',
            componentName:'login'
        }
    }, {
        path: '/index',
        name: 'index',
        component: () => import('@/views/index/index.vue'),
        meta: {
            title: 'route.index.meta.title',
            auth:['admin','user'],
            componentName:'index'
        }
    },
    page1,
    page2,
    page3,
]

const router = createRouter({
    history: createWebHistory(),
    routes,
})
let routeArr = router.getRoutes().splice(1)

router.beforeEach(async (to, from, next) => {

    // console.log(store,'1111111111111111111111')
    // 判断是否有权限
    let auth = localStorage.getItem('myAuth')
    routeArr.forEach(item => {
        if(item.path == to.fullPath){
            console.log('(!item.meta.auth?.includes(auth!)',(!item.meta.auth?.includes(auth!)))
            console.log('(!item.meta.auth',(item.meta.auth))
            console.log('(auth!)',auth)
            if(!item.meta.auth?.includes(auth!)){
                ElMessage({
                    message: '无权限访问',
                    type: 'warning',
                })
                next('/index');
            }
        }
    });
    if (!localStorage.getItem('myAdmin') && to?.path != '/') {

        // 将用户重定向到登录页面
        console.log('未登录')
        ElMessage({
            message: '用户未登录',
            type: 'warning',
        })
        next('/');
    }else{
        next();
    }
})

export default router;


